﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    public class UserRoleDao : BaseDao<UserRole>
    {

        #region instance

        private static UserRoleDao? instance;
        public static UserRoleDao Instance => instance ??= new UserRoleDao();

        public override string GetTableName()
        {
            return TableName.sys_user_role.ToString();
        }

        #endregion


        public int Insert(UserRole usrrRole)
        {
            string sql = SqlBuilder.GetInsertSql(usrrRole, GetTableName());
            return Sqlite.Insert(sql);
        }

        public int Update(UserRole usrrRole)
        {
            string sql = SqlBuilder.GetUpdateSql(usrrRole, GetTableName());
            return Sqlite.Update(sql);
        }

        public List<UserRole> GetListByUserId(long userId)
        {
            string sql = $"select * from {GetTableName()} where {UserRoleField.user_id} ={userId};";
            var data = Sqlite.Select<UserRole>(sql);
            return data;
        }

        public List<UserRole> GetListByRoleId(long roleId)
        {
            string sql = $"select * from {GetTableName()} where {UserRoleField.role_id} ={roleId};";
            var data = Sqlite.Select<UserRole>(sql);
            return data;
        }
        /// <summary>
        /// 删除之前的，增后来的
        /// </summary>
        /// <param name="userid">用户Id</param>
        /// <param name="userRoles"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Update(long userid, List<UserRole> userRoles)
        {
            // userid ==1 为超管 不可操作
            if (userid <= 0 || userid == 1) return 0;
            string delSql = SqlBuilder.GetDeleteSql(GetTableName(), $"{UserRoleField.user_id} = {userid}");
            if (userRoles == null || userRoles.Count == 0)
            {
                //dele old
                return Sqlite.Delete(delSql);
            }
            string[] Sqls = new string[userRoles.Count + 1];

            Sqls[0] = delSql;

            for (int i = 1; i <= userRoles.Count; i++)
            {
                string inSql = SqlBuilder.GetInsertSql(userRoles[i - 1], GetTableName());
                Sqls[i] = inSql;
            }
            int res = Sqlite.TransactionExecute(Sqls);
            return res;
        }
        public int DeleteByUserId(long userId)
        {
            string sql = SqlBuilder.GetDeleteSql(GetTableName(), $"{UserRoleField.user_id} ={userId}");
            return Sqlite.Delete(sql);
        }

        public int DeleteByRoleId(long roleId)
        {
            string sql = SqlBuilder.GetDeleteSql(GetTableName(), $"{UserRoleField.role_id} ={roleId}");
            return Sqlite.Delete(sql);
        }
        /// <summary>
        /// 是否被使用
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool IsUsed(long roleId)
        {
            string sql = $"select {UserRoleField.role_id} from {GetTableName()} where {UserRoleField.role_id} ={roleId};";
            var data = Sqlite.Select<UserRole>(sql);
            return data.Count > 0;
        }
    }
}
